//
//  DYTSubViewController.m
//  BaiSi
//
//  Created by mac on 16/10/17.
//  Copyright © 2016年 DeYi. All rights reserved.
//

#import "DYTSubViewController.h"
#import "DYSubTagCell.h"
#import <AFNetworking/AFNetworking.h>
#import <SVProgressHUD/SVProgressHUD.h>

#import <MJExtension/MJExtension.h>

#import "DYSubTagItem.h"



@interface DYTSubViewController ()
@property(nonatomic,strong) NSMutableArray *arrays;

@property(nonatomic,weak) AFHTTPSessionManager *mgr;

@end



@implementation DYTSubViewController

    static NSString * const ID = @"subCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"推荐标签";
   
    [self.tableView registerNib:[UINib nibWithNibName:@"DYSubTagCell" bundle:nil] forCellReuseIdentifier:ID];
    
//    self.tableView.allowsSelection = NO;
    
    //提示正在加载
    [SVProgressHUD showWithStatus:@"正在努力加载中......"];
    
    //加载数据
    [self loadData];
    
    //设置底部视图
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = [UIColor grayColor];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}


-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
    [_mgr.tasks makeObjectsPerformSelector:@selector(cancel)];
}

#pragma mark - 加载数据
- (void)loadData {
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    _mgr = mgr;
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    param[@"a"] = @"tag_recommend";
    param[@"action"] = @"sub";
    param[@"c"] = @"topic";
    
    [mgr GET:DYBaseUrl parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSArray *  _Nullable responseObject) {
        
        [SVProgressHUD dismiss];
//        LHLog(@"%@",responseObject);
//        [responseObject writeToFile:@"/Users/mac/Desktop/BaiSi/SubTag.plist" atomically:YES];
        //字典转模型
        self.arrays = [DYSubTagItem mj_objectArrayWithKeyValuesArray:responseObject];
        
        //刷新表格
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
        LHLog(@"%@",error);
        
    }];
    
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _arrays.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    DYSubTagCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    cell.item = _arrays[indexPath.row];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 61;
}


@end
